﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Configuration;

namespace qdata
{
    /// <summary>
    /// 总结数据
    /// </summary>
    public   class TotalData
    {

        private static volatile TotalData m_instance = null;

        public static TotalData GetInstance()
        {
            if (m_instance == null)
            {
                lock (typeof(TotalData))
                {
                    if (m_instance == null)
                    {

                        m_instance = new TotalData();
                    }
                }
            }
            return m_instance;
        }



        public  void Doinfo(string[] SArr ,string[] EArr,string site)
        {
            string DongcheShowdata = "";
            string huocheShowdata = "";
            string content = string.Empty;
            GetCarStation gc = new GetCarStation();
             string jttemp = ReaFile("jt.txt");
            foreach (string sCity in SArr)
            {
                foreach (string ECity in EArr)
                {
                    DataTable dtinfo = gc.ExecuteDataTable(string.Format("select * from [TotalInfo] where Scity ='{0}' and  Ecity = '{1}'", sCity, ECity));
                    if (dtinfo != null && dtinfo.Rows.Count > 0)
                    {
                        content += ChangeTempData(dtinfo.Rows[0], jttemp);
                        huocheShowdata = GetAllHuoche(sCity, ECity, site);
                        DongcheShowdata = GetAllDHuoche(sCity, ECity, site);
                        content  = content.Replace("|CHECILIST|", huocheShowdata + DongcheShowdata) + "\r\n \r\n\r\n";
                        string htmlsite=  SiteData.GetInstance().GetOtherInfo(site);
                        content = content.Replace("|OTHERLIST|", htmlsite);


                    }
                }
            }

            Write("ask1.txt", content);
        }

        public string GetAllHuoche(string scity, string ecity, string site)
        {
          
            int HSeqno = 0;
            string distanct = string.Empty;
            string huocheShowdata=string.Empty ;
          
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity));
            string huocetemp = ReaFile("huocetemp.txt");
            if (dt != null)
            {
                DataRow[] DArr = dt.Select("sstation not  like '%D%'");

                if (DArr != null && DArr.Length > 0)
                {
                    for (int i = 0; i < DArr.Length; i++)
                    {
                        DArr[i]["seqno"] = (i + 1).ToString();
                        huocheShowdata += ChangeTempData(DArr[i], huocetemp);
                        if ((i) > int.Parse(ConfigurationSettings.AppSettings["HuocheCount"].ToString())) break;
                     }
                  }
              }
             return huocheShowdata;
          }


        public string GetAllDHuoche(string scity, string ecity, string site)
        {
            int HSeqno = 0;
            string distanct = string.Empty;
            string huocheShowdata = string.Empty;
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity));
            string huocetemp = ReaFile("huocetemp.txt");
            if (dt != null)
            {
                DataRow[] DArr = dt.Select("sstation   like '%D%'");

                if (DArr != null && DArr.Length > 0)
                {
                    for (int i = 0; i < DArr.Length; i++)
                    {
                        DArr[i]["seqno"] = (i + 1).ToString();
                        huocheShowdata += ChangeTempData(DArr[i], huocetemp);
                        if ((i) > int.Parse(ConfigurationSettings.AppSettings["HuocheCount"].ToString())) break;
                    }
                }
            }
            return huocheShowdata;
        }
        public void DoTotal(string[] scity, string[] ecity, string site)
        {
            foreach (string s in scity)
            {
                foreach (string s1 in ecity)
                {

                    DoTotal(s, s1, site);
                }
            }
        }

        public void DoTotal( string scity, string ecity ,string site)
        {
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity));
            if (dt == null || dt.Rows.Count <= 0) return;
            string AllTotal = dt.Rows.Count.ToString();

            DataRow[] Ddata= dt.Select("checi Like '%D%'");
            string DTtotal = Ddata.Length.ToString() ;
            string HCtotal = (dt.Rows.Count - Ddata.Length).ToString() ;
            string distanct = dt.Rows[0]["lucheng"].ToString();


          
            DataTable dt1 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity)  + " order by sdate asc  ");
            DataTable dt2 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'",  scity, ecity) + " order by sdate desc   ");
            if (dt1 == null || dt1.Rows.Count <= 0) return;
            string ZCheci = dt1.Rows[0]["checi"].ToString();
            string ZDate = dt1.Rows[0]["sdate"].ToString();
            string LCheci = dt2.Rows[0]["checi"].ToString();
            string LDate = dt2.Rows[0]["sdate"].ToString();



            DataTable dt3 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity) + " order by piaojia asc  ");
            DataTable dt4 = gc.ExecuteDataTable(string.Format("select top 1 * from huochetab where sstation like '%{0}%' and  estation like '%{1}%'", scity, ecity) + " order by piaojia desc   ");

            string Cheapest = dt3.Rows[0]["piaojia"].ToString();
            string chpcheci = dt3.Rows[0]["checi"].ToString();
            string expensive = dt4.Rows[0]["piaojia"].ToString();
            string expensivecheci = dt4.Rows[0]["checi"].ToString();

            DataTable dttemp = gc.ExecuteDataTable(string.Format("select * from [TotalInfo] where Scity ='{0}' and  Ecity = '{1}'", scity, ecity));
            if (dttemp != null && dttemp.Rows.Count > 0)
            {
                string ustr = "update [TotalInfo] set Distance='{0}',  ZCheci='{1}',ZDate='{2}',LCheci='{3}',LDate='{4}' , Total='{5}',  DTotal='{6}' , HTotal='{7}',Cheapest='{10}',chpcheci='{11}',expensive='{12}',expensivecheci='{13}',EndSite='{14}'  where Scity ='{8}' and  Ecity = '{9}'";

                ustr = string.Format(ustr, distanct, ZCheci, ZDate, LCheci, LDate, AllTotal, DTtotal, HCtotal, scity, ecity, Cheapest, chpcheci, expensive, expensivecheci, site);
                gc.ExecuteDataTable(ustr);
            }
            else
            {
                string ustr = "insert into [TotalInfo] (Distance ,ZCheci ,ZDate,LCheci,LDate,Total,DTotal,HTotal,Scity,Ecity ,Cheapest,chpcheci,expensive,expensivecheci,EndSite)  values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')";

                ustr = string.Format(ustr, distanct, ZCheci, ZDate, LCheci, LDate, AllTotal, DTtotal, HCtotal, scity, ecity, Cheapest, chpcheci, expensive, expensivecheci, site);
                gc.ExecuteDataTable(ustr);
            }


        }

        public string ChangeTempData(DataRow dr, string datatemp)
        {
            DataTable dt = dr.Table;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                datatemp = datatemp.Replace("|" + dt.Columns[i].ColumnName.ToUpper() + "|", dr[dt.Columns[i].ColumnName].ToString());

            }
            return datatemp;
        }

        public string GetBus(string Scity,string Ecity  )
        {
            string bustemp = ReaFile("bustemp.txt");
            string temp = "";
            GetCarStation gc = new GetCarStation();
            DataTable dt = gc.ExecuteDataTable(string.Format("select a.*, b.Address,b.Descrtion,b.Phone from Carbanci as a left join CarStation as b on a.StationName=b.Name where a.Scity like '%{0}%' and  a.ECity like '%{1}%'", Scity, Ecity));
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    temp += ChangeTempData(dt.Rows[i], bustemp);
                }
            }
            return temp;
        }

        public string ReaFile(string filename)
        {
            string str5 = Application.StartupPath;
            StreamReader sr = File.OpenText(@str5 + "\\" + filename);
            string nextLine;
            string content = string.Empty;
            while ((nextLine = sr.ReadLine()) != null)
            {
                content += nextLine + "\n";
            }
            sr.Close();
            return content;
        }


        public void Write(string filename,string content)
        {
            string str5 = Application.StartupPath;
            FileInfo finfo = new FileInfo(@str5 + "\\" + filename);
            using (FileStream fs = finfo.OpenWrite())
            {
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
                sw.WriteLine(content);
                sw.Flush();
                sw.Close();

            }
        }


    }
}
